var express = require('express');
var router = express.Router();
const multiparty = require('multiparty')
const fs = require('fs')
const path = require('path')

//上传图片
router.get('/uploadImg', function (req, res, next) {
    var id = req.query.id;
    res.render('admin_tmpl/uploadImg.html', {id: id});
});

//富文本
router.get('/richText', function (req, res, next) {
    var id = req.query.id;
    res.render('admin_tmpl/richText.html', {id: id});
});


/*
 * 上传文件
 * @return
 *
 */
router.post("/upload/addFile", async (req, res, next) => {
    var randomCode = common.prototype.getRandomCode(6);
    var numberCode = common.prototype.getNumberCode(6);
    let form = new multiparty.Form();
    form.encoding = 'utf-8';
    form.uploadDir = './public/upload';
    form.parse(req, function (err, fields, files) {
        try {
            let inputFile = files.file[0];
            let path = inputFile.path;
            let indx = path.lastIndexOf('.')
            let postfix = path.substring(indx)
            let folder = ''
            if (fields.folder) {
                folder = fields.folder[0] + "/";
            }
            var resName = folder + "img-" + randomCode + "-" + numberCode + postfix;
            let newPath = form.uploadDir + "/" + resName;
            fs.renameSync(inputFile.path, newPath);
            res.send({
                code: 0, msg: '上传成功',
                data: "http://localhost:8080/upload/" + resName,
                name: resName
            });
        } catch (err) {
            console.log(err);
            res.send({
                code: 404, msg: "上传失败！"
            });
        }
        ;
    })
})


/*
 * 富文本上传图片
 * @return
 *
 */
router.post("/upload/article/addFile", async (req, res, next) => {
    var randomCode = common.prototype.getRandomCode(6);
    var numberCode = common.prototype.getNumberCode(6);
    let form = new multiparty.Form();
    form.encoding = 'utf-8';
    form.uploadDir = './public/upload/article';
    form.parse(req, function (err, fields, files) {

        try {
            let inputFile = files.upload[0];
            let path = inputFile.path;
            let originalFilename = inputFile.originalFilename;
            let indx = path.lastIndexOf('.')

            let newPath = form.uploadDir + "/" + originalFilename;
            fs.renameSync(path, newPath);
            res.send({
                // "error" : 0,
                // "url" : "http://localhost:8080/upload/article/" + resName,
                "uploaded": 1,
                "url": "http://localhost:8080/upload/article/" + originalFilename
                // "error" : 0,
                // "data": [
                //     {
                //         url: "http://localhost:8080/upload/article/" + resName,
                //         alt: "",
                //         href: ""
                //     }
                // ]
            });
        } catch (err) {
            console.log(err);
            res.send({
                "uploaded": 0,
                "error": {"message": "上传失败"}
            });
        }
        ;

    })
})

/*
 * 轮播图上传图片
 * @return
 *
 */
router.post("/upload/carouselImages/addFile", async (req, res, next) => {

    var randomCode = common.prototype.getRandomCode(6);
    var numberCode = common.prototype.getNumberCode(6);
    let form = new multiparty.Form();
    form.encoding = 'utf-8';
    form.uploadDir = './public/upload/carouselImages';
    form.parse(req, function (err, fields, files) {
        try {
            let inputFile = files.file[0];
            let path = inputFile.path;
            let indx = path.lastIndexOf('.')
            let postfix = path.substring(indx)
            var resName = "img-" + randomCode + "-" + numberCode + postfix;
            let newPath = form.uploadDir + "/" + resName;
            fs.renameSync(inputFile.path, newPath);
            res.send({
                code: 0, msg: '上传成功',
                data: "http://localhost:8080/upload/carouselImages/" + resName,
                name: resName
            });
        } catch (err) {
            console.log(err);
            res.send({
                code: 404, msg: "上传失败！"
            });
        }
    })
})


/*
 * 产品上传图片
 * @return
 *
 */
router.post("/upload/product/addFile", async (req, res, next) => {

    var randomCode = common.prototype.getRandomCode(6);
    var numberCode = common.prototype.getNumberCode(6);
    let form = new multiparty.Form();
    form.encoding = 'utf-8';
    form.uploadDir = './public/upload/product';
    form.parse(req, function (err, fields, files) {
        try {
            let inputFile = files.file[0];
            let path = inputFile.path;
            let indx = path.lastIndexOf('.')
            let postfix = path.substring(indx)
            var resName = "img-" + randomCode + "-" + numberCode + postfix;
            let newPath = form.uploadDir + "/" + resName;
            fs.renameSync(inputFile.path, newPath);
            res.send({
                code: 0, msg: '上传成功',
                data: "http://localhost:8080/upload/product/" + resName,
                name: resName
            });
        } catch (err) {
            console.log(err);
            res.send({
                code: 404, msg: "上传失败！"
            });
        }
    })
})

/*
 * 新闻上传图片
 * @return
 *
 */
router.post("/upload/news/addFile", async (req, res, next) => {

    var randomCode = common.prototype.getRandomCode(6);
    var numberCode = common.prototype.getNumberCode(6);
    let form = new multiparty.Form();
    form.encoding = 'utf-8';
    form.uploadDir = './public/upload/news';
    form.parse(req, function (err, fields, files) {
        try {
            let inputFile = files.file[0];
            let path = inputFile.path;
            let indx = path.lastIndexOf('.')
            let postfix = path.substring(indx)
            var resName = "img-" + randomCode + "-" + numberCode + postfix;
            let newPath = form.uploadDir + "/" + resName;
            fs.renameSync(inputFile.path, newPath);
            res.send({
                code: 0, msg: '上传成功',
                data: "http://localhost:8080/upload/news/" + resName,
                name: resName
            });
        } catch (err) {
            console.log(err);
            res.send({
                code: 404, msg: "上传失败！"
            });
        }
    })
})

/*
 * 上传文件
 * @return
 *
 */
router.post("/upload/file/addFile", async (req, res, next) => {
    let form = new multiparty.Form();
    form.encoding = 'utf-8';
    form.uploadDir = './public';
    form.parse(req, function (err, fields, files) {
        try {
            let inputFile = files.file[0];
            let path = inputFile.path;
            let originalFilename = inputFile.originalFilename;
            let newPath = form.uploadDir + "/" + originalFilename;
            fs.renameSync(path, newPath);
            res.send({
                code: 0, msg: '上传成功',
                data: "http://localhost:8080/" + originalFilename,
                name: originalFilename
            });
        } catch (err) {
            console.log(err);
            res.send({
                code: 404, msg: "上传失败！"
            });
        }
        ;
    })
})

module.exports = router;